package com.lyw.home

import android.content.Context
import androidx.databinding.ObservableInt
import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.blankj.utilcode.util.LogUtils
import com.lyw.db.DatabaseProvider
import com.lyw.db.QuestionType
import com.lyw.home.bean.BaseQuestion
import com.lyw.home.bean.QuestionBean
import com.lyw.home.bean.QuestionShortBean
import kotlinx.coroutines.launch
import java.io.InputStreamReader

class QuestionVm : ViewModel(),QuestionAdapter.OnAnswerSelectedListener {
//    private var _contents: MutableStateFlow<List<String>> = MutableStateFlow(mutableListOf())
//    var contents: StateFlow<List<String>> = _contents.asStateFlow()

    //作者
//    private var _author: MutableLiveData<String> = MutableLiveData("")
//    var author: LiveData<String> = _author
//    private var _poetyTitle: MutableLiveData<String> = MutableLiveData("")
//    var poetyTitle: LiveData<String> = _poetyTitle
    var a = ObservableInt(0)


    private val _questions = MutableLiveData<List<BaseQuestion>>()
    val questions: LiveData<List<BaseQuestion>> = _questions
    val pageSize = 5
    var pageNumber = 1
    var offset = (pageNumber - 1) * pageSize
    private val _itemChanged:MutableLiveData<Int> = MutableLiveData<Int>(0)
    val itemChanged:LiveData<Int> = _itemChanged



    fun nextQuestion(){


    }
    private fun loadJsonFromAsset(context: Context,filename: String): String {
        val inputStream = context.assets.open(filename)
        val reader = InputStreamReader(inputStream, "UTF-8")
        val bufferedReader = reader.buffered()
        return bufferedReader.use { it.readText() }
    }
    fun doLoadMore(context: Context){
        viewModelScope.launch {

        }
    }
    fun  initData(topType: String,subType:String,knowledgeType:Int){
        viewModelScope.launch {
            var questionEntities = QuestionRepository.getQuestionsByType(topType,subType,knowledgeType)
            _questions.value = questionEntities.map {
                var question:BaseQuestion
                if(it.type==QuestionType.Selection.ordinal){
                    question=QuestionBean(it.id,it.type,QuestionType.values().getOrNull(it.type)?.typeName?:"",
                        it.option.split("|").toList(),it.answer,it.question)

                }else{
                    question=
                        QuestionShortBean(it.id,it.type,QuestionType.values().getOrNull(it.type)?.typeName?:"",
                        it.answer,it.question,false)
                }
                question.question =  (questionEntities.indexOf(it)+1).toString()+"/"+questionEntities.size +"."+question.question
                question
            }
        }
    }
    fun initDb(context: Context) {


    }

    override fun onAnswerSelected(question: QuestionBean, selectedOptionIndex: Int) {
        question.option = selectedOptionIndex
        question.isCorrect = question.options.getOrNull(selectedOptionIndex) == question.answer
        if (question.isCorrect) {
            question.optionDes =  "正确"
        } else {
            question.optionDes =  "错误"
        }
        _questions.value?.find {
            it.id == question.id
        }?.let {
            _itemChanged.value = _questions.value?.indexOf(it)
        }
    }

}